import java.util.HashMap;
import java.util.Map;
import java.util.Random;

/**
 *  线程范围内的共享变量的实现
 */
public class ThreadScopeShareData {

	private static int data = 0;
	private static Map<Thread,Integer> threadData = new HashMap<Thread,Integer>();
	
	public static void main(String[] args) {
		for(int i=0;i<2;i++){
			new Thread(new Runnable(){
				public void run() {
					int data = new Random().nextInt();
					threadData.put(Thread.currentThread(), data);
					System.out.println(Thread.currentThread().getName()+
							" put data :"+data);
					new A().get();
					new B().get();
				}
			}).start();
		}
	}
	
	static class A {
		public int get() {
			int data = threadData.get(Thread.currentThread());
			System.out.println("A "+ Thread.currentThread().getName()+ " get data:"+data);
			return data;
		}
	}
	
	static class B {
		public int get() {
			int data = threadData.get(Thread.currentThread());
			System.out.println("B "+ Thread.currentThread().getName()+ " get data:"+data);
			return data;
		}
	}

}
